<?php
/**
 * 微信接口
 *
 */
class WeixinAction extends HomeAction
{
    private $ToUserName="";
    private $FromUserName="";
    public function index(){
        $this->valid();
        $this->responseMsg();
    }
    
    public function valid()
    {
    
        $echoStr = $_GET["echostr"];
    
        if($this->checkSignature()){
    
            echo $echoStr;
    
            exit;
    
        }
    
    }
    
    public function responseMsg()
    
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    
        if (!empty($postStr))
        {
    
           	$xmlObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
    
           	$this->ToUserName=$xmlObj->ToUserName;
    
           	$this->FromUserName=$xmlObj->FromUserName;
    
           	$CreateTime=$xmlObj->CreateTime;
    
           	$MsgType=$xmlObj->MsgType;
    
           	$Content=$xmlObj->Content;
           	
           	$event=$xmlObj->Event;
    
           	if($MsgType=="text"){
           	    //文本回复
           	    if($Content==1){
           	        $title="认识一种男士生活方式：OWNONLY定制男装";
           	        $content="匠心定制完美时尚OWNONLY是一个以成衣价格提供定制男装，在中、美、法、日开展零售的新锐品牌，中国市场由浙";
           	        $img_url=url()."/statics/images/weixin/3.jpg";
           	        $url="http://mp.weixin.qq.com/s?__biz=MzA3MTM2NjM5MQ==&mid=205185991&idx=1&sn=9fa545dfd72c20d57be06c174e60d299#rd";
           	        echo $this->send_pic($this->FromUserName,$this->ToUserName,$title,$content,$img_url,$url);
           	    }elseif($Content==2){
           	        $str="联系我们
官方网站：www.ownonly.com
具体地址
杭州：杭州市下城区长城街22号205室；联系电话：400-888-1532；客服邮箱：service@ownonly.net
湖南：湖南省长沙市开福区万达广场A座17003:；联系电话：18907488661；QQ:3115789769   
日本：名古屋店〒497-0058愛知県海部郡蟹江町富吉１丁目５３６MIYOSHI BLD 2F
美国：4482 Barranca Pkwy, Suite 228, Irvine,California,CA92604";
           	        echo $this->send_text($this->FromUserName,$this->ToUserName,$str);
           	    }
           	}
           
           	elseif($MsgType=="event")
            { //事件回复
               if($event=="subscribe"||$event=="SCAN")
               {//关注回复
           	        $str="
 欢迎在OWNONLY官方微信开始品质男士生活！这里可买定制西装、衬衫、配饰；可学搭配知识；可把玩男士品味------

OWNONLY是一家在中、美、法、日开展零售的新锐品牌，致力于提供高性价比的私人定制男装，打造一站式的高性价比、便捷体验的男士穿衣解决方案。

输入以下数字进行查询
1：品牌介绍
2：联系我们
欢迎给小编留言了解更多资讯哦~
           	            ";
           	        echo $this->send_text($this->FromUserName,$this->ToUserName,$str);
           	   }
           	   else if($event=="CLICK")
               {//自定义菜单回复
                    if($xmlObj->EventKey=="txkf")
           	        
           	        {
           	            $title="欢迎联系我们";
           	            $content="客服电话：400-888-1532客服邮箱：service@ownonly.net";
           	            $img_url=url()."/statics/images/weixin/0.jpg";
           	            $url="http://mp.weixin.qq.com/s?__biz=MzA3MTM2NjM5MQ==&mid=208024530&idx=1&sn=5f4d330988bc7d8de36cf596193345c0&scene=18#rd";
           	            echo $this->send_pic($this->FromUserName,$this->ToUserName,$title,$content,$img_url,$url);
           	        }elseif($xmlObj->EventKey=="mflt"){
           	            $title="【OWNONLY男装定制】为您私人定制，预约量体 400-888-1532";
           	            $content="为您私人定制男士高档西服，预约上门量体服务！ 预约上门电话：400-888-1532 微信预约：请在微信公众号输入 [我要预约量体]";
           	            $img_url=url()."/statics/images/weixin/1.jpg";
           	            $url="http://mp.weixin.qq.com/s?__biz=MzA3MTM2NjM5MQ==&mid=400353456&idx=1&sn=11dd79e82300e4eab68e2c6dbeee40fc&scene=18#rd";
           	            echo $this->send_pic($this->FromUserName,$this->ToUserName,$title,$content,$img_url,$url);
           	        }elseif($xmlObj->EventKey=="xffs"){
           	            $title="奢华的西装 最好是定制 ";
           	            $content="Savile Row，伦敦Oxford Circus附近的一条小街，这条拥有两百多年悠久历史的小街上，从头到";
           	            $img_url=url()."/statics/images/weixin/0.jpg";
           	            $url="http://mp.weixin.qq.com/s?__biz=MzA3MTM2NjM5MQ==&mid=208028319&idx=1&sn=50d0750d053af46f578826ba1cb9cbbd&scene=18#rd";
           	            echo $this->send_pic($this->FromUserName,$this->ToUserName,$title,$content,$img_url,$url);
           	        }elseif($xmlObj->EventKey=="csfs"){
           	            $title="衬衫如何跟裤子优雅的搭配 ";
           	            $content="衬衫和裤子搭配模式只有三种模式——塞进去、塞一半、不塞。但是只知道这些还不够，每一种模式如何达到最佳状态，需";
           	            $img_url=url()."/statics/images/weixin/2.jpg";
           	            $url="http://mp.weixin.qq.com/s?__biz=MzA3MTM2NjM5MQ==&mid=208027642&idx=1&sn=efbeaf9fa1f3d564ce77e1ce51e95589&scene=18#rd";
           	            echo $this->send_pic($this->FromUserName,$this->ToUserName,$title,$content,$img_url,$url);
           	        }
           	        
           	        
               } 
    
    
           	}
    
        }
    
    }
    private function checkSignature()
    {
        $signature = $_GET["signature"];
    
        $timestamp = $_GET["timestamp"];
    
        $nonce = $_GET["nonce"];
    
        $token = C('weixin_token');
    
        $tmpArr = array($token, $timestamp, $nonce);
    
        sort($tmpArr);
    
        $tmpStr = implode( $tmpArr );
    
        $tmpStr = sha1( $tmpStr );
    
        if( $tmpStr == $signature ){
    
            return true;
    
        }else{
    
            return false;
    
        }
    
    }
    //获得凭证接口
    public function access_token() {
        
        if(cache('weixin_access_token')){
    
            $cont=cache('weixin_access_token');
    
            return json_decode($cont, 1);
    
        }else{
    
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".C('weixin_appid')."&secret=".C('weixin_secret');

            $cont = file_get_contents($url);
    
            cache('weixin_access_token',$cont,7200);
            
            return json_decode($cont, 1);
        }
    
    }
    //创建微信菜单
    public function weixin_menu() {
         $data = '{
    
         "button":[
         {
        
            "name":"孤平台",
            "sub_button":[
              {
        
              "type":"view",
        
              "name":"开始定制",
        
              "url":"http://www.ownonly.net"
              },
              {
        
              "type":"click",
        
              "name":"调戏客服",
        
              "key":"txkf"
            }]
        
          },
        
         {	
          "type":"view",
          "name":"预约量体",
          "url":"http://www.ownonly.net/schedule"
          },
        
          {
        
               "name":"品风尚",
        
               "sub_button":[
        
               {
        
                   "type":"view",
        
                   "name":"衣来故事",
        
                   "url":"http://www.ownonly.net/article/about_us"
        
                },
        
                {
        
                   "type":"click",
        
                   "name":"西服风尚",
        
                   "key":"xffs"
        
                },
        
                {
        
                   "type":"click",
        
                   "name":"衬衫风尚",
        
                   "key":"csfs"
        
                },
				{
        
                   "type":"view",
        
                   "name":"加入我们",
        
                   "url":"http://www.ownonly.net/article/appointments"
        
                },
				{
        
                   "type":"view",
        
                   "name":"如何定制",
        
                   "url":"http://www.ownonly.net/article/how_customize"
        
                },
                
				]
        
           }]
        
     }';
    
        $access_token =  $this->access_token();
    
        //print_r($access_token);
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL,'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $access_token['access_token']);
    
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        $data = curl_exec($ch);
    
        print_r($data);//创建成功返回：{"errcode":0,"errmsg":"ok"}
    }
    //推送文本消息
    public function send_text($fromUserName,$toUserName,$content){
    
        $str="
                <xml>
    
                <ToUserName><![CDATA[%s]]></ToUserName>
    
                <FromUserName><![CDATA[%s]]></FromUserName>
    
                <CreateTime>%s</CreateTime>
    
                <MsgType><![CDATA[text]]></MsgType>
    
                <Content><![CDATA[%s]]></Content>
    
                </xml>
    
                ";
    
        return $result=sprintf($str,$fromUserName,$toUserName,time(),$content);
    
    }
    
    //推送图文消息
    public function send_pic($fromUserName,$toUserName,$title,$content,$picurl,$url){
       $str="<xml>
    
	        <ToUserName><![CDATA[".$fromUserName."]]></ToUserName>
    
			<FromUserName><![CDATA[".$toUserName."]]></FromUserName>
    
			<CreateTime>".time()."</CreateTime>
    
			<MsgType><![CDATA[news]]></MsgType>
    
			<ArticleCount>1</ArticleCount>
    
			<Articles>
    
            <item>
    
			<Title><![CDATA[".$title."]]></Title>
    
			<Description><![CDATA[".$content."]]></Description>
    
			<PicUrl><![CDATA[".$picurl."]]></PicUrl>
    
			<Url><![CDATA[".$url."]]></Url>
    
			</item>
    
            </Articles>
    
			</xml>
    
	 	";
    
        return $str;
    
    }
     
    
}